﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetX.Translation;

namespace DotNetX
{
    /// <summary>
    /// A list of all supported languages
    /// </summary>
    public class Languages
    {
        /// <summary>
        /// This returns the default langauge
        /// </summary>
        public static Language DefaultLanguage
        {
            get
            {
                // return the default language
                return (from link in LanguageInCountries.Links
                        where link.Country.Name.ToLower().Trim() == "united kingdom"
                        select link.Language).First();
            }
        }

        /// <summary>
        /// The supported languages
        /// </summary>
        public static List<Language> AllLanguages
        {
            get
            {
                // return the languages
                return _languages;
            }
        }
        #region Languages

        private static List<Language> _languages = new List<Language>(
            new Language[] {
                new Language("Afar", "AA"),
                new Language("Abkhazian", "AB"),
                new Language("Afrikaans", "AF"),
                new Language("Amharic", "AM"),
                new Language("Arabic", "AR"),
                new Language("Assamese", "AS"),
                new Language("Aymara", "AY"),
                new Language("Azerbaijani", "AZ"),
                new Language("Bashkir", "BA"),
                new Language("Byelorussian", "BE"),
                new Language("Bulgarian", "BG"),
                new Language("Bihari", "BH"),
                new Language("Bislama", "BI"),
                new Language("Bengali", "BN"),
                new Language("Bangla", "BN"),
                new Language("Tibetan", "BO"),
                new Language("Breton", "BR"),
                new Language("Catalan", "CA"),
                new Language("Chinese (Traditional) - Cantonese", "漢語", "CH"),
                new Language("Chinese (Simplified) - Mandarin", "汉语", "CHS"),
                new Language("Corsican", "CO"),
                new Language("Czech", "CS"),
                new Language("Welsh", "CY"),
                new Language("Danish", "DA"),
                new Language("German", "DE"),
                new Language("Bhutani", "DZ"),
                new Language("Greek", "EL"),
                new Language("English", "EN"),
                new Language("Esperanto", "EO"),
                new Language("Spanish", "Español", "ES"),
                new Language("Estonian", "ET"),
                new Language("Basque", "EU"),
                new Language("Persian", "FA"),
                new Language("Finnish", "FI"),
                new Language("Fiji", "FJ"),
                new Language("Faeroese", "FO"),
                new Language("French", "FR"),
                new Language("Frisian", "FY"),
                new Language("Irish", "GA"),
                new Language("Gaelic", "GD"),
                new Language("Galician", "GL"),
                new Language("Guarani", "GN"),
                new Language("Gujarati", "GU"),
                new Language("Hausa", "HA"),
                new Language("Hindi", "HI"),
                new Language("Croatian", "HR"),
                new Language("Hungarian", "HU"),
                new Language("Armenian", "HY"),
                new Language("Interlingua", "IA"),
                new Language("Interlingue", "IE"),
                new Language("Inupiak", "IK"),
                new Language("Indonesian", "IN"),
                new Language("Icelandic", "IS"),
                new Language("Italian", "IT"),
                new Language("Hebrew", "IW"),
                new Language("Japanese", "JA"),
                new Language("Yiddish", "JI"),
                new Language("Javanese", "JW"),
                new Language("Georgian", "KA"),
                new Language("Kazakh", "KK"),
                new Language("Greenlandic", "KL"),
                new Language("Cambodian", "KM"),
                new Language("Kannada", "KN"),
                new Language("Korean", "KO"),
                new Language("Kashmiri", "KS"),
                new Language("Kurdish", "KU"),
                new Language("Kirghiz", "KY"),
                new Language("Latin", "LA"),
                new Language("Lingala", "LN"),
                new Language("Laothian", "LO"),
                new Language("Lithuanian", "LT"),
                new Language("Latvian", "LV"),
                new Language("Lettish", "LV"),
                new Language("Malagasy", "MG"),
                new Language("Maori", "MI"),
                new Language("Macedonian", "MK"),
                new Language("Malayalam", "ML"),
                new Language("Mongolian", "MN"),
                new Language("Moldavian", "MO"),
                new Language("Marathi", "MR"),
                new Language("Malay", "MS"),
                new Language("Maltese", "MT"),
                new Language("Burmese", "MY"),
                new Language("Nauru", "NA"),
                new Language("Nepali", "NE"),
                new Language("Dutch", "NL"),
                new Language("Norwegian", "NO"),
                new Language("Occitan", "OC"),
                new Language("Oromo", "OM"),
                new Language("Afan", "OM"),
                new Language("Oriya", "OR"),
                new Language("Punjabi", "PA"),
                new Language("Polish", "PL"),
                new Language("Pashto", "PS"),
                new Language("Pushto", "PS"),
                new Language("Portuguese", "PT"),
                new Language("Quechua", "QU"),
                new Language("Rhaeto-Romance", "RM"),
                new Language("Kirundi", "RN"),
                new Language("Romanian", "RO"),
                new Language("Russian", "RU"),
                new Language("Kinyarwanda", "RW"),
                new Language("Sanskrit", "SA"),
                new Language("Sindhi", "SD"),
                new Language("Sangro", "SG"),
                new Language("Serbo-Croatian", "SH"),
                new Language("Singhalese", "SI"),
                new Language("Slovak", "SK"),
                new Language("Slovenian", "SL"),
                new Language("Samoan", "SM"),
                new Language("Shona", "SN"),
                new Language("Somali", "SO"),
                new Language("Albanian", "SQ"),
                new Language("Serbian", "SR"),
                new Language("Siswati", "SS"),
                new Language("Sesotho", "ST"),
                new Language("Sudanese", "SU"),
                new Language("Swedish", "SV"),
                new Language("Swahili", "SW"),
                new Language("Tamil", "TA"),
                new Language("Tegulu", "TE"),
                new Language("Tajik", "TG"),
                new Language("Thai", "TH"),
                new Language("Tigrinya", "TI"),
                new Language("Turkmen", "TK"),
                new Language("Tagalog", "TL"),
                new Language("Setswana", "TN"),
                new Language("Tonga", "TO"),
                new Language("Turkish", "TR"),
                new Language("Tsonga", "TS"),
                new Language("Tatar", "TT"),
                new Language("Twi", "TW"),
                new Language("Ukrainian", "UK"),
                new Language("Urdu", "UR"),
                new Language("Uzbek", "UZ"),
                new Language("Vietnamese", "VI"),
                new Language("Volapuk", "VO"),
                new Language("Wolof", "WO"),
                new Language("Xhosa", "XH"),
                new Language("Yoruba", "YO"),
                new Language("Zulu", "ZU") });

        #endregion

        /// <summary>
        /// This returns all languages by thier name
        /// </summary>
        /// <param name="name">The name of the language</param>
        /// <returns>The languages that match the passed name</returns>
        public static List<Language> GetByName(string name)
        {
            if (name == null)
                throw new ArgumentNullException("'name' is null.");
            if (name.Length == 0)
                throw new ArgumentOutOfRangeException("'name' is zero-length.");

            // trim the name
            name = name.Trim().ToLower();

            // return the languages
            return (from l in AllLanguages
                    where l.Name.Trim().ToLower().StartsWith(name)
                    select l).ToList();
        }

        /// <summary>
        /// This returns all languages by their code
        /// </summary>
        /// <param name="code">The code</param>
        /// <returns>The languages that match the passed code</returns>
        public static List<Language> GetByCode(string code)
        {
            if (code == null)
                throw new ArgumentNullException("'code' is null.");
            if (code.Length == 0)
                throw new ArgumentOutOfRangeException("'code' is zero-length.");

            // trim the code
            code = code.Trim().ToLower();

            // return the languages
            return (from l in AllLanguages
                    where l.Code.Trim().ToLower() == code
                    select l).ToList();
        }

        /// <summary>
        /// This returns the language translation text indicated by the passed key of the passed language
        /// </summary>
        /// <param name="targetLanguage">The language to get the translation of</param>
        /// <param name="key">The key of the translation to get</param>
        /// <returns>The translated text, string.Empty upon error</returns>
        public static string GetTranslationByKey(Language targetLanguage, string key)
        {
            if (targetLanguage == null)
                throw new ArgumentNullException("'targetLanguage' is null.");
            if (key == null)
                throw new ArgumentNullException("'key' is null.");
            if (key.Length == 0)
                throw new ArgumentOutOfRangeException("'key' is zero-length.");

            // return the value
            if (HackedTranslationObject.Translation != null)
                return HackedTranslationObject.Translation.GetTranslatedText(targetLanguage, key);
            else
                return key;
        }
    }
}
